<?php

namespace app\modules\web\controllers;

use yii\web\Controller;
use \app\common\services\ConstantMapService;
use \app\modules\web\controllers\common\BaseController;
use \app\models\book\BookCat;
use \app\models\book\Book;
use \app\models\book\Images;
class BookController extends BaseController
{
  
    public function actionIndex()
    {
    	$this->layout="main";
        $query=Book::find();
        //获取get参数
        $status=trim($this->get("status","-1"));
        $mix_kw=trim($this->get("mix_kw",""));
        $cat_id=trim($this->get("cat_id",""));
        //status条件
        if($status > ConstantMapService::$status_default){
            $query->andWhere(["status"=>$status]);
        }
        //分类条件
        if($cat_id>1){
            $query->andWhere(['cat_id'=>$cat_id]);
        }
        //到这儿
        if($mix_kw){
            $name=["LIKE","name","%".$mix_kw."%",false];
            $query->andWhere("OR",[$name]);
        }
        //分页
        $p=($this->get("p",1));

        $total_all=$query->count();
        $page_size=2;
        $total_page=ceil($total_all/$page_size);
        $list=$query->orderBy(['id'=>SORT_DESC])
                ->offset( ($p-1)*$page_size )
                ->limit($page_size)
                ->all();
        $status_mapping=ConstantMapService::$status_mapping;
        return $this->render('index',["list"=>$list,'status_mapping'=>$status_mapping,'page'=>[
                'total_all'=>$total_all,
                'total_page'=>$total_page,
                'page_size'=>$page_size,
                'p'=>$p
            ]]);
        // $list = Book::find()->joinWith(['bookCat'])->select('book.id,book.name,book.price,book.stock,book.tags,book_cat.name as cat_name,book.cat_id,book.status')->asArray()->all();
       
        // $list=Book::find()->all();
        return $this->render('index',['list'=>$list]);
    }
    //图书添加,更新
    public function actionSet()
    {
    	$this->layout="main";
        if(\Yii::$app->request->isGet){
             //查找类别
            $list=BookCat::find()->all();
            $id=$this->get("id","");
            $info=null;
            if($id){
                $info=Book::find()->where(['id'=>$id])->one();

            }
            return $this->render('set',['info'=>$info,'list'=>$list]);
        }
        $cat_id=$this->post("cat_id","");
        $name=$this->post("name","");
        $price=$this->post("price","");
        $editor=$this->post("editor","");
        $stock=$this->post("stock","");
        $tags=$this->post("tags","");
        $main_image=$this->post("image_key","");
        $id=$this->post("id","");
        //判读是否找到
        $is_find=Book::find()->where(['name'=>$name])->andWhere(['<>','id',$id])->one();
        if($is_find){
            return $this->renderJson([],"图书名称存在",-1);
        }
        //添加图片
        $new_images=new Images();
        $new_images->file_key=$main_image;
        $new_images->bucket=$this->post("bucket","");
        $new_images->created_time=time();
        $new_images->save(0);
        $info=Book::find()->where(['id'=>$id])->one();
        if($info){
            $new_book=$info;
            $new_book->updated_time=time();
            $new_book->name=$name;

            $new_book->price=$price;
            $new_book->cat_id=$cat_id;
            $new_book->summary=$editor;
            $new_book->main_image=$main_image;
            $new_book->stock=$stock;
            $new_book->tags=$tags;
        }else{
            $new_book=new Book();
            $new_book->main_image=$main_image;
            $new_book->name=$name;

            $new_book->price=$price;
            $new_book->cat_id=$cat_id;
            $new_book->summary=$editor;
            $new_book->stock=$stock;
            $new_book->tags=$tags;
            $new_book->created_time=time();
            $new_book->updated_time=time();
        }
        $new_book->save(0);
        return $this->renderJson([],"操作成功");
        
    }
    public function actionInfo()
    {
    	$this->layout="main";
        return $this->render('info');
    }
    public function actionImg()
    {
    	$this->layout="main";
        $list=Images::find()->orderBy(['id'=>SORT_DESC])->all();
        
        return $this->render('img',['list'=>$list]);
    }
    
    public function actionCat()
    {
    	$this->layout="main";
        $status=$this->post("status","-1");
        $query=BookCat::find();
        if($status > ConstantMapService::$status_default){
            $query->where(['status'=>$status]);
        }
        $list=$query->orderBy(['id'=>SORT_DESC])->all();
        // var_dump($list);
        return $this->render('cat',['list'=>$list]);
    }
    public function actionCat_set()
    {
    	$this->layout="main";
        if(\Yii::$app->request->isGet){
            $id=$this->get("id","");
            $info=null;
            if($id){
                $info=BookCat::find()->where(['id'=>$id])->one();

            }
            return $this->render('cat_set',['info'=>$info]);
        }
        $name=$this->post("cat_name","");
        $weight=$this->post("weight","");
        $id=$this->post("id","");
        //到这儿
        $is_find=BookCat::find()->where(['name'=>$name])->andWhere(['<>','id',$id])->one();
        if($is_find){
            return $this->renderJson([],"分类存在",-1);
        }
        $info=BookCat::find()->where(['id'=>$id])->one();
        if($info){
            $book_cat=$info;
            $book_cat->updated_time=time();
            $book_cat->name=$name;
            $book_cat->weight=$weight;

        }else{
            $book_cat=new BookCat();
            $book_cat->name=$name;
            $book_cat->weight=$weight;
            $book_cat->status=1;
            $book_cat->created_time=time();
            $book_cat->updated_time=time();
        }
        $book_cat->save(0);
        return $this->renderJson([],"操作成功");

    }
    //图书删除,恢复操作
    public function actionOps(){
        if(!\Yii::$app->request->isPost){
            return $this->renderJson([],"系统繁忙~请稍后再试~",-1);
        }
        $id=intval($this->post("id",0));
        $act=trim($this->post("act",""));
        if(!$id){
            return $this->renderJson([],"操作有误~",-1);
        }
        if(!in_array($act,["remove","recover"])){
            return $this->renderJson([],"操作有误~",-1);
        }
        $book_info=Book::find()->where(["id"=>$id])->one();
        if(!$book_info){
            return $this->renderJson([],"图书不存在~",-1);
        }
        switch($act){
            case "remove":
                $book_info->status=0;
                break;
            case "recover":
                $book_info->status=1;
                break;
                
        }
        $book_info->updated_time=date("Y-m-d H:i:s");
        $book_info->update(0);
        return $this->renderJson([],"编辑成功~");
    }
    //分类删除,恢复操作
    public function actionCatOps(){
        if(!\Yii::$app->request->isPost){
            return $this->renderJson([],"系统繁忙~请稍后再试~",-1);
        }
        $id=intval($this->post("id",0));
        $act=trim($this->post("act",""));
        if(!$id){
            return $this->renderJson([],"操作有误~",-1);
        }
        if(!in_array($act,["remove","recover"])){
            return $this->renderJson([],"操作有误~",-1);
        }
        $book_cat_info=BookCat::find()->where(["id"=>$id])->one();
        if(!$book_cat_info){
            return $this->renderJson([],"分类不存在~",-1);
        }
        switch($act){
            case "remove":
                $book_cat_info->status=0;
                break;
            case "recover":
                $book_cat_info->status=1;
                break;
                
        }
        $book_cat_info->updated_time=date("Y-m-d H:i:s");
        $book_cat_info->update(0);
        return $this->renderJson([],"编辑成功~");
    }
}
